﻿@model OT.Areas.Student.Models.ExamData
@{
    ViewBag.Title = "Online Test Details";
    Layout = "~/Areas/Student/Views/Shared/_Layout.cshtml";
}
@Scripts.Render("~/bundles/timer")
@*<script src="~/js/DisableSelectin.js"></script>*@
<style type="text/css">
#defaultCountdown { width: 240px; height: 45px; }
#dvLoading
{
   background:#fff url(/../img/Fillingbrokenring.gif) no-repeat center center;
   height: 100px;
   width: 100px;
   position: fixed;
   z-index: 1000;
   left: 50%;
   top: 50%;
   margin: -25px 0 0 -25px;
}
    </style>
<div id="dvLoading" >Loading....</div>
<div id="defaultCountdown"></div>
@*<div id="tick"></div>*@
@using (Html.BeginForm("", "", null, FormMethod.Get, new { id = "formQue" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.Duration, new { id = "HdnTimer" })
    <div class="matter" >
        <div class="container">
            <div class="row">
                <div class="col-md-9" style="flex-align: center;">
                    <div class="widget-content">
                        <div id="quest">
                            <table style='width: 100%;'>
                                @Html.HiddenFor(model => model.CurrentQuestion,new { id = "hdnCurrentQuestion" })
                                @Html.HiddenFor(model => model.TotalQuestion,new { id = "hdnTotalQuestion" })
                                @Html.HiddenFor(model => model.TestID,new { id = "hdnTestID" })
                                @Html.HiddenFor(model => model.QuestionID,new { id = "hdnQuestionID" })
                                <tr>
                                    <td colspan="2">
                                        <hr />
                                        <b style="padding-right: 10px">Question <span id="spanQuestionNo"></span>:</b>
                                        <span id="spanQuestionName" style="font-size: 20px;">@Html.DisplayTextFor(model => model.QuestionName)</span>
                                        <hr />
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 20px">@Html.RadioButton("marks", "1", true, new { id = "rdbtnAnswer1" })</td>
                                    <td>
                                        <div id="divAnswer1">@Html.DisplayTextFor(model => model.Answer1)</div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>@Html.RadioButton("marks", "2", new { id = "rdbtnAnswer2" })</td>
                                    <td>
                                        <div id="divAnswer2">@Html.DisplayTextFor(model => model.Answer2)</div>
                                    </td>
                                </tr>
                                <tr id="transwer3">
                                    <td>@Html.RadioButton("marks", "3", new { id = "rdbtnAnswer3" })</td>
                                    <td>
                                        <div id="divAnswer3">@Html.DisplayTextFor(model => model.Answer3)</div>
                                    </td>
                                </tr>
                                <tr id="transwer4">
                                    <td>@Html.RadioButton("marks", "4", new { id = "rdbtnAnswer4" })</td>
                                    <td>
                                        <div id="divAnswer4">@Html.DisplayTextFor(model => model.Answer4)</div>
                                    </td>
                                </tr>
                            </table>
                            <br />
                        </div>
                        <div id="ex"></div>
                        <br />
                        <br />
                        <div>
                            <a id="lnkbtnprevious" href="javascript:void(0)" class="btn btn-success" style="margin-left: 5px">Previous</a>
                            <a id="lnkbtnnext" href="javascript:void(0)" class="btn btn-success" style="margin-left: 5px">Next</a>
                            <a id="lnkbtnskip" href="javascript:void(0)" class="btn btn-warning" style="margin-left: 5px">Skip</a>
                            <a id="lnkbtnendTest" href="javascript:void(0)" class="btn btn-danger">End Test</a>
                        </div>
                        <div>
                            <span style="margin-top: 20px; margin-bottom: 20px; display: none">Question <span id="curque"></span>of <span id="totque"></span></span>
                            <br />
                        </div>
                    </div>
                </div>
                <div class="col-md-3" style="text-align: right;">
                    <div style="width: 100%; height: 100%; margin: 10px 15px 15px 10px;" id="links" class="col-md-3 pagination">
                    </div>
                    <div style="text-align: right;">
                        <span class="label label-success">Answered</span>
                        <span class="label label-warning">Skip</span>
                        <span class="label" style="color: gray; border: 0.5px solid gray">UnVisited</span>
                        <span class="label label-default">Current</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
}
@Styles.Render("~/Content/timer")
@Scripts.Render("~/bundles/timer")

@section scripts{
    <script type="text/javascript">
        (function ($) {
            $.fn.disableSelection = function () {
                return this
				.attr('unselectable', 'on')
				.css('user-select', 'none')
				.css('-moz-user-select', 'none')
				.css('-khtml-user-select', 'none')
				.css('-webkit-user-select', 'none')
				.on('selectstart', false)
				.on('contextmenu', false)
				.on('keydown', false)
				.on('mousedown', false);
            };
            //$.fn.enableSelection = function () {
            //    return this
            //    .attr('unselectable', '')
            //    .css('user-select', '')
            //    .css('-moz-user-select', '')
            //    .css('-khtml-user-select', '')
            //    .css('-webkit-user-select', '')
            //    .off('selectstart', false)
            //    .off('contextmenu', false)
            //    .off('keydown', false)
            //    .off('mousedown', false);
            //};
        })(jQuery);
        $(window).load(function () {
            $('#dvLoading').fadeOut(2000);
        });
        function alertTimeout(wait) {
            setTimeout(function () {
                $('#alert_placeholder').children('.alert:first-child').remove()
            }, wait);
        }

        bootstrap_alert = function () { }

        bootstrap_alert.danger = function (message) {
            $('#alert_danger').html('<span class="alert alert-danger"><strong>' + "Message! " + '</strong><span>' + message + '</span></span>')
        }

        $(document).keydown(function (e) {
            if (((e.which || e.keyCode) != 116) || ((e.which || e.keyCode) != 82)) {
                return false;
            }
        });

        document.onkeydown = getKeyCode;
        var keyCode = 0;
        var skipQue = Array();
        var AttemptID1 = 0;
        var i = 0;
        var Position = 0;
        var MaxPosition = 0;
        window.onbeforeunload = confirmExit;

        function confirmExit() {
            if (keyCode == 116) {
                keyCode = 0;
                // For F5
                return "You have attempted to leave this test. Are you sure you want to leave the test?";
            }
            else {
                // For close browser
                return "You have attempted to leave this test. Are you sure you want to leave the test?";
            }
        }

        function getKeyCode(e) {
            if (window.event) {
                e = window.event;
                keyCode = e.keyCode;
            }
            else {
                keyCode = e.which;
            }
        }
        function HideExtraRadioButton() {
            if ($("#divAnswer3").html() == "" || $("#divAnswer3").html() == null) {
                $("#rdbtnAnswer3").hide();
                $("#rdbtnAnswer4").hide();
            }
            else if ($("#divAnswer4").html() == "" || $("#divAnswer4").html() == null) {
                $("#rdbtnAnswer3").show();
                $("#rdbtnAnswer4").hide();
            }
            else {
                $("#rdbtnAnswer3").show();
                $("#rdbtnAnswer4").show();
            }
        }
        function clearCheckBox() {
            $("#rdbtnAnswer1").prop('checked', false);
            $("#rdbtnAnswer2").prop('checked', false);
            $("#rdbtnAnswer3").prop('checked', false);
            $("#rdbtnAnswer4").prop('checked', false);
        }

        function setactive(pos) {
            $(".pagination ul li").each(function (index, value) {
                if (Number(index) == Number(pos - 1)) {
                    $(this).removeClass("skip");
                    $(this).removeClass("answer");
                    $(this).addClass("active");
                }
            });
        }

        function setanswer(pos) {
            $(".pagination ul li").each(function (index, value) {
                if (Number(index) == Number(pos - 1)) {
                    if ($(this).hasClass("skip"))
                    { $(this).removeClass("skip"); }
                    $(this).addClass("answer");
                }
            });
        }

        function setskip(pos) {
            $(".pagination ul li").each(function (index, value) {
                if (Number(index) == Number(pos - 1)) {
                    if ($(this).hasClass("active"))
                    { $(this).removeClass("active"); }
                    $(this).addClass("skip");
                }
            });
        }

        function firstload()
        {
            debugger; 
            var TestID = $('#hdnTestID').val();
            var UserID = getCookie("LoginSesID");
             var QuestionID = $('#hdnQuestionID').val();
             var ChoosenAns = SetSelectedAnswer();
             $.get("/Student/Available/GetQuestionAndAnswer", { TestID: TestID, UserID: UserID }, function (allData) {
                 AttemptID1 = allData.allData[0].AttemptID;
                 var mydiv = $("#links");
                 var spanTag = document.createElement('ul');
                 mydiv.prepend(spanTag);
                 $.each(allData.quedata, function (index, value) {
                     var aTag = document.createElement('a');
                     var liTag = document.createElement('li');
                     aTag.setAttribute('class', "queNumber");
                     aTag.setAttribute('href', "javascript:void(0)");
                     aTag.innerHTML = index + 1;
                     aTag.id = value;
                     spanTag.appendChild(liTag);
                     liTag.appendChild(aTag);
                 });
                 setactive(1);
                 $('#HdnTimer').val(allData.allData[0].Duration);
                 $("#hdnQuestionID").val(allData.allData[0].QuestionID);
                 $("#spanQuestionName").text(allData.allData[0].QuestionName);
                 $("#divAnswer1").html(allData.allData[0].Answer1);
                 $("#divAnswer2").html(allData.allData[0].Answer2);
                 $("#divAnswer3").html(allData.allData[0].Answer3);
                 $("#divAnswer4").html(allData.allData[0].Answer4);
                 $('#hdnQuestionID').val(allData.allData[0].QuestionID);
                 $('#hdnTestID').val(allData.allData[0].TestID);
                 HideExtraRadioButton();
                 MaxPosition = allData.allData.length;
                 // Set Time For Timer Control
                 $('#defaultCountdown').countdown({ until: (allData.allData[0].Duration*60), onExpiry: showmodelforend, expiryText: '<div style="color:red;font-size:20px;text-align:center">It\'s all over</div>' });

             });
        }
        function ManageLastPrevBtn(Curr, Max) {
            if (Max == Curr) {
                $("#lnkbtnnext").css({ "display": "none" });
            }
            else {
                $("#lnkbtnnext").css({ "display": "inline-block" });
            }
            if (Curr == 1) {
                $("#lnkbtnprevious").css({ "display": "none" });
            }
            else {
                $("#lnkbtnprevious").css({ "display": "inline-block" });
            }
        }

        function Initializelabel() {
            $('#h2Title').html("<i class='fa  fa-tachometer'></i> Test Start");
            $('#aTitle').html('<a href="./index/">Available Test </a>' + '<a href="./ContinueTest?TestID=@Session["id"]">/ Countinue Tests / </a>' + " Start Test");
            $('#liAvailable').addClass("open");
        }
      
        $(document).ready(function () {
            $('body').disableSelection();
                Initializelabel();
                clearCheckBox();
                firstload();
            });
        
        function SetSelectedAnswer()
            {
                var ChoosenAns = 0;
                var check = $("#rdbtnAnswer1").is(':checked');
                var check1 = $("#rdbtnAnswer2").is(':checked');
                var check2 = $("#rdbtnAnswer3").is(':checked');
                var check3 = $("#rdbtnAnswer4").is(':checked');
                if (check == true) { ChoosenAns = $("#rdbtnAnswer1").val(); }
                if (check1 == true) { ChoosenAns = $("#rdbtnAnswer2").val(); }
                if (check2 == true) { ChoosenAns = $("#rdbtnAnswer3").val(); }
                if (check3 == true) { ChoosenAns = $("#rdbtnAnswer4").val(); }
                $("#rdbtnAnswer1").prop('checked', false);
                $("#rdbtnAnswer2").prop('checked', false);
                $("#rdbtnAnswer3").prop('checked', false);
                $("#rdbtnAnswer4").prop('checked', false);
                return ChoosenAns;
            }

        function SetGivenAnswer(ans) {
                if (ans == 1) { $("#rdbtnAnswer1").prop('checked', true); }
                if (ans == 2) { $("#rdbtnAnswer2").prop('checked', true); }
                if (ans == 3) { $("#rdbtnAnswer3").prop('checked', true); }
                if (ans == 4) { $("#rdbtnAnswer4").prop('checked', true); }
            }
           
        $("#lnkbtnnext").click(function (e) {
            $('#alert_danger').children('.alert:first-child').remove();
            var op = document.getElementsByClassName('queNumber');
            if (Position == 0) {
                Position = Position + 1;
            }
                var TestID = $('#hdnTestID').val();
                var AttemptID = AttemptID1;
                var QuestionID = op[Position-1].id;
                var ChoosenAns = SetSelectedAnswer();
                if (ChoosenAns != 0) {
                    $.get("/Student/Available/GetQuestionAndAnswer1", { AttemptID: AttemptID, QuestionID: QuestionID, TestID: TestID, ButtonType: 'Next', ChoosenAns: ChoosenAns, Position: Position }, function (datatest) {
                        debugger;
                        $("#hdnQuestionID").val(datatest.datatest.QuestionID);
                        var flag = QuestionID;
                        $("#spanQuestionName").text(datatest.datatest.QuestionName);
                        $("#divAnswer1").html(datatest.datatest.Answer1);
                        $("#divAnswer2").html(datatest.datatest.Answer2);
                        $("#divAnswer3").html(datatest.datatest.Answer3);
                        $("#divAnswer4").html(datatest.datatest.Answer4);
                        HideExtraRadioButton();
                        for (var i = 0; i < datatest.data1.length - 1 ; i++) {
                            if (datatest.data1[i].YourAnswer > 0) {
                                if (datatest.data1[i].QuestionID == flag) {
                                    var t = '#' + flag;
                                    var ind = $(t).text();
                                    SetGivenAnswer(datatest.data1[i].YourAnswer);
                                    setanswer(ind);
                                    if (Number(skipQue[i]) == Position) {
                                        skipQue.splice(i, 1);
                                    }
                                    //check value for position
                                }
                                else {
                                    setanswer(i + 1);
                                }
                            }
                            else {
                                if (datatest.data1[i].YourAnswer == null) {
                                    setskip(i + 1);
                                }
                            }
                        }
                    });
                    debugger;
                    clearCheckBox();
                    setactive(Position + 1);
                    ManageLastPrevBtn(Position + 1, MaxPosition);
                    $("#spanQuestionNo").html(Position + 1);
                    Position = Position + 1;
                }
                else {
                    $("#ex").after(' <span id="alert_danger"></span>');
                    bootstrap_alert.danger('Please Select Answer');
                }
        });

        $("#lnkbtnprevious").click(function (e) {
            $('#alert_danger').children('.alert:first-child').remove();
            var TestID = $('#hdnTestID').val();
            var AttemptID = AttemptID1;
                var QuestionID = $('#hdnQuestionID').val();
                var ChoosenAns = SetSelectedAnswer();
                if (ChoosenAns != 0) {
                    $.get("/Student/Available/GetQuestionAndAnswer1", { AttemptID: AttemptID, QuestionID: QuestionID, TestID: TestID, ButtonType: 'Prev', ChoosenAns: ChoosenAns, Position: Position }, function (datatest) {
                      
                        $("#hdnQuestionID").val(datatest.datatest.QuestionID);
                        var flag = datatest.datatest.QuestionID;
                        $("#spanQuestionName").text(datatest.datatest.QuestionName);
                        $("#divAnswer1").html(datatest.datatest.Answer1);
                        $("#divAnswer2").html(datatest.datatest.Answer2);
                        $("#divAnswer3").html(datatest.datatest.Answer3);
                        $("#divAnswer4").html(datatest.datatest.Answer4);
                        HideExtraRadioButton();
                        for (var i = 0; i < datatest.data1.length ; i++) {
                            if (datatest.data1[i].YourAnswer > 0) {
                                if (datatest.data1[i].QuestionID == flag) {
                                    SetGivenAnswer(datatest.data1[i].YourAnswer);
                                }
                                setanswer(i + 1);
                            }
                            else {
                                setskip(i + 1);
                            }
                        }
                    });
                    setactive(Position - 1);
                    Position = Position - 1;
                    ManageLastPrevBtn(Position, MaxPosition);
                    $("#spanQuestionNo").html(Position);
                }
                else {
                    $("#ex").after(' <span id="alert_danger"></span>');
                    bootstrap_alert.danger('Please Select Answer');
                }
                
        });

        $(document).on("click", ".queNumber", null, function (e) {
            debugger;
            if (Position == 0) {
                Position = Position + 1;
            }
            var TestID = $('#hdnTestID').val();
            var AttemptID = AttemptID1;
            var QuestionID = this.id;
            var position = this.text;
            var PassPosition = Number(position) - 1;
           var Position1 = Number(position);
            if (Position == MaxPosition) {
                skipQue.push(MaxPosition);
                setskip(MaxPosition);
            }
            else {
                if (Position == 1) {
                    skipQue.push(Position);
                    setskip(Position);
                }
                 skipQue.push(Position);
                 setskip(Position);
            }
                var ChoosenAns = SetSelectedAnswer();
                $.get("/Student/Available/GetQuestionAndAnswer1", { AttemptID: AttemptID, QuestionID: QuestionID, TestID: TestID, ButtonType: 'QueNumber', ChoosenAns: ChoosenAns, Position: PassPosition }, function (datatest) {
                    $("#hdnQuestionID").val(datatest.datatest.QuestionID);
                    var flag = datatest.datatest.QuestionID;
                    $("#spanQuestionName").text(datatest.datatest.QuestionName);
                    $("#divAnswer1").html(datatest.datatest.Answer1);
                    $("#divAnswer2").html(datatest.datatest.Answer2);
                    $("#divAnswer3").html(datatest.datatest.Answer3);
                    $("#divAnswer4").html(datatest.datatest.Answer4);
                    HideExtraRadioButton();
                    for (var i = 0; i < datatest.data1.length ; i++) {
                        if (datatest.data1[i].YourAnswer > 0) {
                            if (datatest.data1[i].QuestionID == flag) {
                                SetGivenAnswer(datatest.data1[i].YourAnswer);
                            }
                            setanswer(i + 1);
                        }
                    }
                });
                debugger;
                $("#spanQuestionNo").html(Position1);
                ManageLastPrevBtn(Position1, MaxPosition);
                setactive(Position1);
                Position = Position1;
        });

        $("#lnkbtnskip").click(function (e) {
            debugger;
            if (Position == 0) {
                Position = Position + 1;
            }
            skipQue.push(Position);
            setskip(Position);
            if (Position == MaxPosition || Position > MaxPosition) {
                Position = 0;
            }
            var TestID = $('#hdnTestID').val();
            var AttemptID = AttemptID1;
            var QuestionID = $('#hdnQuestionID').val();
            var ChoosenAns = SetSelectedAnswer();
            $.get("/Student/Available/GetQuestionAndAnswer1", { AttemptID: AttemptID, QuestionID: QuestionID, TestID: TestID, ButtonType: 'skip', ChoosenAns: ChoosenAns, Position: Position }, function (datatest) {
                 $("#hdnQuestionID").val(datatest.datatest.QuestionID);
                var flag = datatest.datatest.QuestionID;
                $("#spanQuestionName").text(datatest.datatest.QuestionName);
                $("#divAnswer1").html(datatest.datatest.Answer1);
                $("#divAnswer2").html(datatest.datatest.Answer2);
                $("#divAnswer3").html(datatest.datatest.Answer3);
                $("#divAnswer4").html(datatest.datatest.Answer4);
                HideExtraRadioButton();
                for (var i = 0; i < datatest.data1.length ; i++) {
                    if (datatest.data1[i].YourAnswer > 0) {
                        if (datatest.data1[i].QuestionID == flag) {
                            SetGivenAnswer(datatest.data1[i].YourAnswer);
                            setactive(i + 1);
                        }
                        else {
                            setanswer(i + 1);
                        }
                   }
                }
            });
            debugger;
            setactive(Position + 1);
            ManageLastPrevBtn(Position + 1, MaxPosition);
            $("#spanQuestionNo").html(Position+1);
            Position = Position + 1;
        });

        $("#lnkbtnendTest").click(function (e) {
            debugger;
            window.onbeforeunload = null;
            showmodelforend();
           
        });

        function showmodelforend() {
            $("#ensTestModel").modal({
                backdrop: 'static',
                keyboard: true
            },'show');
        }

        $("#btnTestFinish").click(function (e) {
            var op = document.getElementsByClassName('queNumber');
            Position = Position + 1;
            var TestID = $('#hdnTestID').val();
            var AttemptID = AttemptID1;
            var QuestionID = $('#hdnQuestionID').val();// op[Position - 1].id;
            var ChoosenAns = SetSelectedAnswer();
            $.get("/Student/Available/GetQuestionAndAnswer1", { AttemptID: AttemptID, QuestionID: QuestionID, TestID: TestID, ButtonType: 'End', ChoosenAns: ChoosenAns,Position: Position }, function (datatest) {
                $("#hdnQuestionID").val('');
                $("#spanQuestionName").text('');
                $("#divAnswer1").html('');
                $("#divAnswer2").html('');
                $("#divAnswer3").html('');
                $("#divAnswer4").html('');

                window.location = "/Student/Available/EndTest?AttemptID=" + getCookie("AttemptID");
            });
           
          });
        
    </script>
}